import Apis from "@/apis"
import ApiFetchers from "@/apis/api_fetchers"
import { createRepository } from "@/utils/zustand"

const [useUserInfo, _state_atoms_userInfo] = createRepository(ApiFetchers.refGetProfile, {
    authRequired: true,
    persist: true,
    key: "userInfo",
    persistOptions: {
        storage: localStorage,
    },
})
const [useKyc, _state_atoms_kyc] = createRepository(ApiFetchers.getKyc, {
    authRequired: true,
    persist: true,
    key: "kyc",
    persistOptions: {
        storage: localStorage,
    },
})

const [useBrandList, _state_atoms_brandList] = createRepository(ApiFetchers.getBrandList, {
    persist: true,
    key: "brandList",
    persistOptions: {
        storage: localStorage,
    },
})

type RefGameListItem = ApiTypes.Fetcher.Util.ExtractItemFromListApi<typeof Apis.v1.getGameList>
type TypeGameListResult = globalThis.ApiTypes.Fetcher.Result.List<
    RefGameListItem,
    ApiTypes.Fetcher.Props.getGameList | undefined
>

const [useGameList, _state_atoms_gameList] = createRepository<
    ApiTypes.Fetcher.Props.getGameList,
    TypeGameListResult
>(ApiFetchers.getGameList, {
    defaultProps: {
        limit: 10,
        categories: [],
        brands: [],
        cursor: "",
    },
})

const [useClientAssets, _state_atoms_clientAssets] = createRepository(ApiFetchers.getClientAssets)

const [useUserWallet, _state_atoms_userWallet] = createRepository(ApiFetchers.getWallet, {
    authRequired: true,
})

const [useRechargeChannelList, _state_atoms_rechargeChannelList] = createRepository(
    ApiFetchers.getRechargeChannelList,
    {
        authRequired: true,
    },
)

const [useUpdateEmail, _state_atoms_updateEmail] = createRepository<
    ApiTypes.Fetcher.Props.UpdateEmail,
    ApiTypes.Fetcher.Result.UpdateEmailResult
>((props) => {
    if (!props) throw new Error("UpdateEmail props are required")
    return ApiFetchers.updateEmail(props)
})

const [useRefGetProfile, _state_atoms_refGetProfile] = createRepository(
    () => ApiFetchers.refGetProfile().then((res) => res ?? {}),
    {
        authRequired: true,
    },
)

const [useRefGetFinanceWallet, _state_atoms_refGetFinanceWallet] = createRepository(
    () => ApiFetchers.refGetFinanceWallet().then((res) => res ?? {}),
    {
        authRequired: true,
    },
)

const base = {
    useBrandList,
    useGameList,
    useUserWallet,
    useRechargeChannelList,
    useUpdateEmail,
    useRefGetFinanceWallet,
    useRefGetProfile() {
        const userInfo = useRefGetProfile()
        return {
            data: userInfo.data,
            fetch: userInfo.fetch,
            loading: userInfo.state.loading,
            error: userInfo.state.error,
        }
    },
    useUserInfo() {
        const userInfo = useUserInfo()
        return {
            data: userInfo.data,
            fetch: userInfo.fetch,
            loading: userInfo.state.loading,
            error: userInfo.state.error,
        }
    },
    useKyc() {
        const kycInfo = useKyc()
        return {
            data: kycInfo.data,
            fetch: kycInfo.fetch,
            loading: kycInfo.state.loading,
            error: kycInfo.state.error,
        }
    },
    useKycFlat() {
        const _data = useKyc()
        return {
            data: _data.data,
            fetch: _data.fetch,
            loading: _data.state.loading,
            error: _data.state.error,
        }
    },
    useHomeBanners() {
        const clientAssets = useClientAssets()
        return {
            data: clientAssets.data?.homeBanners ?? [],
            fetch: clientAssets.fetch,
            loading: clientAssets.state.loading,
        }
    },
    useHomeGameSections() {
        const clientAssets = useClientAssets()
        return {
            data: clientAssets.data?.homeGameSections ?? [],
            fetch: clientAssets.fetch,
            loading: clientAssets.state.loading,
        }
    },
    useHomeGameCategories() {
        const clientAssets = useClientAssets()
        return {
            data: clientAssets.data?.homeGameCategories ?? [],
            fetch: clientAssets.fetch,
            loading: clientAssets.state.loading,
        }
    },
    useBrandLogo() {
        const clientAssets = useClientAssets()
        return {
            data: clientAssets.data?.brandLogo,
            fetch: clientAssets.fetch,
            loading: clientAssets.state.loading,
        }
    },
}

export const Repository = {
    ...base,
}
